<?php

class GameboardController extends dmq\Controller
{
	protected $useDb = true;

	// Actions.
	const MAKE_MOVE = "makemove";

	public function doControll()
	{
		$this->title( 'Game: ' . $this->view->gameId() );

		$this->CSS( "gameboard" );

		$this->JS( "game/engine" );
		$this->JS( "game/map" );
		$this->JS( "game/tile" );
		$this->JS( "game/game" );
		$this->JS( "game/player" );
		$this->JS( "game/robot" );
		$this->JS( "game/register" );
		$this->JS( "game/registerdeck" );
		$this->JS( "game/gameview" );
		$this->JS( "game/positiondata" );
		$this->JS( "game/conveyorhelpdata" );
		$this->JS( "game/registerview" );
		$this->JS( "game/robotstatusview" );
		$this->JS( "game/turnmovementcollection" );
		$this->JS( "game/run" );

		if ( $this->view->auth()->isLoggedIn() )
		{
			if ( ( $game = $this->model->getGame( $this->view->gameId() ) ) )
			{
				$self = null;
				foreach ( $game->players() as $player )
				{
					if ( $player->user()->id() == $this->view->auth()->user()->id() )
					{
						$self = $player;
					}
				}


				if ( $this->view->action() == self::MAKE_MOVE )
				{
					try
					{
						$this->model->makeMove( $game, $self, $this->view->registerCards() );
					}
					catch ( Exception $e )
					{
						$this->view->error( $e->getMessage() );
					}
				}

				if ( $this->model->readyCheck( $game ) )
				{
					require_once 'php/game/Engine.php';
					$engine = new game\Engine( $game );
					$engine->DoMajorCycle();

					try
					{
							$this->model->prepareNextTurn( $game, $engine->m_currentGame );
					}
					catch ( Exception $e )
					{
						$this->view->error( "Failed preparing next turn." );
					}

					$game = $this->model->getGame( $game->ID() );
				}


				$this->IJS( "window.onload = function() { run( " . $game->id() . ", \"" . $game->map()->tiles() . "\", [" );

				$nPlayers = count( $game->players() );

				foreach ( $game->Players() as $player )
				{
					$this->IJS( '{  "playerID": ' . $player->user()->id() . ',
									"playerName": "' . $player->user()->name() . '",
									"posX" : ' . $player->robot()->posX() . ',
									"posY" : ' . $player->robot()->posY() . ',
									"spawnX" : ' . $player->robot()->spawnPosX() . ',
									"spawnY" : ' . $player->robot()->spawnPosY() . ',
									"direction": ' . $player->robot()->direction() . ',
									"robotType": ' . $player->robot()->type() . ',
									"cards": ' . ( ( $player->user()->id() == $this->view->auth()->user()->id() ) ? '"' . $player->cards() . '"' : '""' ) . ',
									"self": ' . ( ( $player->user()->id() == $this->view->auth()->user()->id() ) ? 'true' : 'false' ) . ' }' );
					if ( --$nPlayers > 0 )
					{
						$this->IJS( "," );
					}
				}

				$this->IJS( "] ) };" );

				$this->view->buildGameboard();
			}

		}
		else
		{
			$this->view->eror( 'You must be logged in to view this game.' );
		}

		$this->view->printErrors( '<p class="error"><span>Error :(</span> _ERROR_</p>' . "\r\n" );
		$this->view->printMessages( '<p class="message"><span>Success \o/</span> _MESSAGE_</p>' . "\r\n" );

	}
}
